<script setup>
import {useRouter} from "vue-router";
import {ref} from 'vue';
import axios from "axios";
import webConfig from "@/GlobalParas.js";
import {message} from "ant-design-vue";
import {decrypt, encrypt} from "@/util/StringOper.js";

let router = useRouter();
let txt_username = ref("");
let txt_psw = ref("");
localStorage.clear();



function loginCheck() {
  let params = {};
  params.user_name = txt_username.value;
  params.psw = txt_psw.value;

  // let info = encrypt("姚磊岳")
  // console.log(info)
  // console.log(decrypt(info))

  let url = webConfig.postUrl + "/systemUser/getSingleUser";
  // console.log(url)

  axios.post(url, params).then((res) => {
    if (res.data !== "") {
      // console.log(res.data)
      // console.log(TextEncoder(res.data.userName))

      localStorage.setItem("userName", encrypt(res.data.userName));
      localStorage.setItem("realName", encrypt(res.data.realName));
      localStorage.setItem("userId",encrypt(res.data.tableKey));
      localStorage.setItem("deptId",encrypt(res.data.deptId));

      axios.post(webConfig.postUrl + "/systemUser/getAllUsers",null).then((res)=>{
        // console.log(JSON.stringify(res.data))
        localStorage.setItem("allSystemUsers",encrypt(JSON.stringify(res.data)))
      })
      getRoutes();
    } else {
      message.error({
        content: () => '用户名和密码不匹配，请重新输入。',
        style: {
          marginTop: '50vh',
          marginLeft: '90vh'
        }
      })
    }
  })
}

function getRoutes() {
  let MainFrameRoute = {
    name: "MainFrame",
    path: "/MainFrame",
    component: () => import("@/MainFrame.vue"),
    children:[
      {
        name: "DashBoard",
        path: "/MainFrame/DashBoard",
        component:()=>import("@/DashBoard.vue")
      }
    ]
  }
  axios.post(webConfig.postUrl + "/systemFunction/getAllMenuFunctions", null).then((res) => {
    let allRoleFunctions = res.data;

    let viewComponents = import.meta.glob('@/views/*/*.vue');
    // console.log(viewComponents);

    for (let singleFunction of allRoleFunctions) {
      if (singleFunction.routerPath !== null) {
        let funObj = {};
        funObj.name = singleFunction.functionName + "_" + singleFunction.tableKey;
        funObj.path = singleFunction.routerPath;

        let functionRouterPath = '/src' + singleFunction.routerPath + '.vue'
        funObj.component = viewComponents[functionRouterPath];
        MainFrameRoute.children.push(funObj)
      }
    }
    // console.log(MainFrameRoute)
    router.addRoute(MainFrameRoute);
    router.push({name:"DashBoard"})
  })
}

</script>

<template>
  <div class="bg">

  </div>
  <div class="whiteBlock">

  </div>
  <div class="loginBlock">
  </div>
  <div class="loginContent">
    <div class="SystemTitle">
      计算机学院年度工作量计分系统 <span style="color:red;font-size:9pt;">v2.0</span>
    </div>
    <div class="inputBlock">
      <div style="color: #3c414a;text-align: center;letter-spacing: 2px">用户登录</div>
      <a-input class="inputType" prefix="用户名：" placeholder="请输入用户名" v-model:value="txt_username"/>
      <a-input-password class="inputType" prefix="密&nbsp&nbsp&nbsp&nbsp码：" placeholder="请输入密码"
                        v-model:value="txt_psw"/>
      <a-button type="primary" class="inputType" @click="loginCheck">登陆系统</a-button>
    </div>
    <div class="TechInfo">
      技术支持：计算机学院瀚岳软件技术团队<br>
      联&nbsp;&nbsp;系&nbsp;人：熊科云（13870094778）
    </div>
    <div class="CopyRightInfo">
      版权所有：江西中医药大学计算机学院
      <br>
      Copyright © 2019~2025.
    </div>
  </div>
</template>

<style scoped>
.bg {
  position: absolute;
  top: 0;
  left: 0;
  background-image: url("@/assets/bg1.jpg");
  background-size: cover;
  width: 100%;
  height: 100%;
  z-index: 10;
}

.whiteBlock {
  position: absolute;
  background: #fff;
  width: 100%;
  height: 300px;
  margin-top: -150px;
  top: 50%;
  z-index: 20;
  opacity: 0.5;
}

.loginBlock {
  position: absolute;
  opacity: 0.5;
  background: #fff;
  width: 360px;
  height: 400px;
  margin-top: -200px;
  top: 50%;
  right: 50px;
  z-index: 30;
  border-radius: 10px;
  border: 1px solid #cccccc;
}

.loginContent {
  position: absolute;
  width: 340px;
  height: 400px;
  margin-top: -200px;
  top: 50%;
  right: 50px;
  z-index: 40;
  border-radius: 10px;
  padding: 10px
}

.SystemTitle {
  color: #0a4ba3;
  font-weight: bold;
  font-size: 18px;
  margin-top: 8px;
}

.inputBlock {
  margin-top: 10px;
  line-height: 200%;
}

.inputType {
  height: 40px;
  font-size: 16px;
  width: 95%;
  margin-left: 5px;
  margin-top: 20px;
}

.TechInfo {
  margin-top: 10px;
  margin-left: 5px;
  line-height: 25px;
}

.CopyRightInfo {
  margin-top: 35px;
  text-align: center;
  font-size: 14px;
  color: #3c414a;
}
</style>